//
//  ClassSubScript.swift
//  Swift3.0_Study
//
//  Created by chengfeng on 2016/9/29.
//  Copyright © 2016年 zhaochengfeng. All rights reserved.
//

import Cocoa

class ClassSubScript: NSObject {

    func start() -> Void {
        //--------------------------------------下属脚本----------------------------
        //下属脚本是访问对象,集合或者序列的快捷方式.开发者不需要调用实例特定的赋值和访问方法,就可以直接访问所需要的数值,
        //例如利用数组的下标去访问和修改数组的某个元素
        //1定义下标脚本
        
        /**
         *  subscript(参数1:数据类型,参数2:数据类型,参数3:数据类型)->返回值数据类型 {
         
         get {
         //返回与参数类型匹配的类型的值
         }
         set(参数名称){
         //执行赋值操作
         }
         
         }
         */
        
        class ClassC {
            var english: Int = 0
            var chinese:Int = 0
            var math:Int = 0
            
            subscript(index:Int) -> Int {
                get {
                    
                    switch index {
                    case 0:
                        return english
                    case 1:
                        return chinese
                    case 2:
                        return math
                    default:
                        return 0
                    }
                    
                }
                set {
                    english = newValue
                    chinese = newValue
                    math = newValue
                    
                }
            }
        }
        
        //2调用下属脚本
        
        let clsC = ClassC()
        
        var sum: Int = 0
        
        for i in 0 ..< 3 {
            sum += clsC[i]
        }
        
        print("修改前的和 \(sum)")
        
        //修改属性的值
        clsC[0] = 100
        clsC[1] = 200
        clsC[2] = 300
        
        
        for i in 0 ..< 3 {
            sum += clsC[i]
        }
        print("修改后的和 \(sum)")
        
        //下属脚本和计算属性一样设置为只读或者致谢
        //只读
        class Sorce {
            var english:Int=130
            var chinese:Int=50
            var math:Int=90
            
            subscript(index:Int)->Int {
                get {
                    
                    switch index {
                    case 0:
                        return english
                    case 1:
                        return chinese
                    case 2:
                        return math
                    default:
                        return 0
                    }
                    
                }
                
            }
        }
        
        let myScore = Sorce()
        
        for j in 0...2 {
            
            print(myScore[j])
        }
        
        //具有多个参数的下标脚本,一般用在多维数组中
        
        class mutiArrayClass {
            
            var rows : Int=0
            var columns = 0
            var grid = [Double]()
            
            init(rows: Int, columns: Int) {
                
                self.rows = rows
                self.columns = columns
                grid = [Double](repeatElement(0.0, count: rows * columns))
            }
            func indexIsValid(row: Int, column: Int) -> Bool {
                
                return row >= 0 && row < rows && column >= 0 && column < columns
            }
            
            subscript(row: Int, column: Int) -> Double {
                
                get {
                    
                    assert(indexIsValid(row: row, column: column), "index out of range")
                    return grid[(row*columns)+column]
                }
                set {
                    
                    assert(indexIsValid(row: row, column: column), "index out of range")
                    return grid[(row*columns)+column]=newValue
                    
                    
                }
            }
        }
        let mutiArr = mutiArrayClass(rows: 2, columns: 2)
        print("没有赋值前")
        print(mutiArr[0,0])
        print(mutiArr[0,1])
        print(mutiArr[1,0])
        print(mutiArr[1,1])
        
        mutiArr[0,0] = 1.4
        mutiArr[0,1] = 53.234
        mutiArr[1,0] = 42.55
        mutiArr[1,1] = 1000.345532
        
        print("赋值后")
        print(mutiArr[0,0])
        print(mutiArr[0,1])
        print(mutiArr[1,0])
        print(mutiArr[1,1])
        
        //下属脚本除了访问对象以及对象的属性外,还可以实现一些自定义的功能
        //一下代码实现的功能计算下标值和10的乘积
        
        class ClassD {
            var count1: Int = 10
            subscript(index: Int) -> Int {
                
                get{
                    let count = index * count1
                    return count
                    
                }
                set {
                    count1 = newValue
                }
                
            }
        }
        
        let clsD = ClassD()
        print(clsD.count1)
        print(clsD[6])
    }
}
